package top.infopub.mgr.mkt.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.mkt.dao.BidBondApplyDao;
import top.infopub.mgr.mkt.dao.BidBondAttDao;
import top.infopub.mgr.mkt.domain.BidBondApplyDomain;
import top.infopub.mgr.mkt.service.BidBondApplyService;
import top.infopub.mgr.mkt.vo.BidBondApplyVo;
import top.infopub.model.mkt.BidBondApply;
import top.infopub.model.mkt.BidBondAtt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class BidBondApplyServiceImpl  implements BidBondApplyService {

	@Autowired
	private BidBondApplyDao bidBondApplyDao;
	
	@Autowired
    private BidBondAttDao bidBondAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;
	

    @Override
    public Page<BidBondApplyDomain> selectPageByDomainWhere(Page<BidBondApplyDomain> page,
                                                        BidBondApplyVo bidBondApplyVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<BidBondApplyDomain>)bidBondApplyDao.selectByDomainWhere(bidBondApplyVo);
    }

    @Override
    public Page<BidBondApplyDomain> selectMyCheckByDomainWhere(Page<BidBondApplyDomain> page,
                                                           BidBondApplyVo bidBondApplyVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<BidBondApplyDomain>)bidBondApplyDao.selectMyCheckByDomainWhere(bidBondApplyVo);
    }

    @Override
    public BidBondApplyVo selectById(String id) {
        return bidBondApplyDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBidBondApply(BidBondApplyVo bidBondApply, List<BidBondAtt> bidBondAtts) {
        //插入预立项记录
        bidBondApplyDao.insertById(bidBondApply);
        
        //批量插入附件
        if(CollectionUtils.isNotEmpty(bidBondAtts)){
            bidBondAttDao.insertByBatch(bidBondAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.MKT_BID_BOND_APPLY;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, bidBondApply.getId(), bidBondApply.getProcessId(), null);
        
    }

    @Override
    public BidBondApply selectByKey(String id) {
        return bidBondApplyDao.selectByKey(id);
    }

}